<?php
require_once($application_folder."/libraries/Merchant.php"); 
class ReturnFail extends CI_Controller 
{

    function __construct() { parent::__construct(); }  

    function index() { $this->test_returnFail(); }

    function test_returnFail (){

        $trans_id = $_POST['trans_id'];
        $error_msg = $_POST['error'];
        //$var = ($_POST['var']); //getting additional parameters
        echo 'Tehnical error occurred! Please contact merchant! <br><br>'.$error_msg;


        $sql = mysql_query("SELECT client_ip_addr FROM transaction WHERE `trans_id` = '".urlencode($trans_id)."'");

        if (!$sql) {
            die('*** Invalid query1: ' . mysql_error());
        }else{
            $row = mysql_fetch_row($sql);
            $client_ip_addr = $row[0];
        }

        $cert = "./certificate/test.pem";
        $password = "testpass";
        $merchant = new Merchant("https://secureshop-test.firstdata.lv:8443/ecomm/MerchantHandler", $cert, $password, 1);

        $resp = $merchant -> getTransResult(urlencode($trans_id), $client_ip_addr);
        $resp = htmlentities($resp, ENT_QUOTES);
        $resp = $error_msg.' + '.$resp;
        $sql = mysql_query("INSERT INTO error VALUES ('', now(), 'ReturnFailURL', '$resp')
        ");

        if (!$sql) {
            die('*** Invalid query2: ' . mysql_error());
        }

    }


}